package com.ebelter.btcomlib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ebelter.btcomlib.AppConfigs;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;

import static com.ebelter.btcomlib.utils.CommonUtils.getAgeByBirthday;
import static com.ebelter.btcomlib.utils.CommonUtils.getBirthdayIntValue;

//这个类只保存用户信息，其他一律不保存
public class UserSpUtil {
    public static final String APP_NAME = "UserSp";
    public static Context mContext;

    public static void init(Context context){
        mContext = context;
    }

    public static String readString(String key) {
        return getSharedPreferences().getString(key, "");
    }

    public static String readString(String key, String value) {
        return getSharedPreferences().getString(key, value);
    }

    public static void writeString(String key, String value) {
        getSharedPreferences().edit().putString(key, value).apply();
    }

    public static boolean readBoolean(String key) {
        return getSharedPreferences().getBoolean(key, false);
    }

    public static boolean readBoolean(String key, boolean defBool) {
        return getSharedPreferences().getBoolean(key, defBool);
    }

    public static void writeBoolean(String key, boolean value) {
        getSharedPreferences().edit().putBoolean(key, value).apply();
    }

    public static int readInt(String key) {
        return getSharedPreferences().getInt(key, 0);
    }

    public static int readInt(String key, int value) {
        return getSharedPreferences().getInt(key, value);
    }

    public static void writeInt(String key, int value) {
        getSharedPreferences().edit().putInt(key, value).apply();
    }

    public static long readLong(String key) {
        return getSharedPreferences().getLong(key, 0);
    }
    public static long readLong(String key,long def) {
        return getSharedPreferences().getLong(key, def);
    }

    public static float readFloat(String key) {
        return getSharedPreferences().getFloat(key, 0);
    }

    public static float readFloat(String key,float def) {
        return getSharedPreferences().getFloat(key, def);
    }

    public static void writeLong(String key, long value) {
        getSharedPreferences().edit().putLong(key, value).apply();
    }
    public static void writeFloat(String key, float value) {
        getSharedPreferences().edit().putFloat(key, value).apply();
    }

    public static void remove(String key) {
        getSharedPreferences().edit().remove(key).apply();
    }

    public static void removeAll() {
        getSharedPreferences().edit().clear().apply();
    }

    private static SharedPreferences getSharedPreferences() {
        return mContext.getSharedPreferences(APP_NAME, Context.MODE_PRIVATE);
    }



    //更新本地数据
    public static void updateLocalData(Map<String, Serializable> originalParameters) {
        if (originalParameters==null){
            return;
        }
        Set<String> sets = originalParameters.keySet();
        if (sets.size()<=0){
            return;
        }
        for (String k:sets){
            if (TextUtils.isEmpty(k)){
                continue;
            }
            Serializable serializable = originalParameters.get(k);
            if (TextUtils.equals(IHttpRequestField.nickName,k)){
                //说明更改了昵称
                UserSpUtil.writeString(Constants.IUser.NICK_NAME, (String) serializable);
            }
            if (TextUtils.equals("height",k)){
                //说明更改了身高
                UserSpUtil.writeInt(Constants.IUser.HEIGHT, (int) serializable);
                ScaleUser.getUser().setHeight((int) serializable);
            }
            if (TextUtils.equals("birthday",k)){
                //说明更改了身高
                UserSpUtil.writeString(Constants.IUser.BIRTHDAY, (String) serializable);

                int year=getBirthdayIntValue((String) serializable,0,1990);
                UserSpUtil.writeInt(Constants.IUser.YEAR,year);


                int mon=getBirthdayIntValue((String) serializable,1,6);
                UserSpUtil.writeInt(Constants.IUser.MONTHS,mon);


                int day=getBirthdayIntValue((String) serializable,2,1);
                UserSpUtil.writeInt(Constants.IUser.DAY,day);


                int age = getAgeByBirthday(year);
                UserSpUtil.writeInt(Constants.IUser.AGE,age);
                ScaleUser.getUser().setAge(age);

            }
        }
    }



}
